package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.web.UserRole;
import com.yituo.mjworld.mvc.entity.web.WebUser;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

@Service
public class QrServiceImpl extends BaseService {

  /**
   * 二维码静默注册用户
   *
   * @param referrerId 推荐人的ID
   * @param sid        浏览器的SID
   * @return 注册成功返回真, 注册失败返回假
   */
  public String register(String referrerId, String sid) {
    System.out.println("referrerId:" + referrerId);
    WebUser referrer = (WebUser) baseDao.get(WebUser.class, referrerId);
    if (referrer == null) {
      return null;
    }
    WebUser newUser = (WebUser) baseDao.get(WebUser.class, sid);
    if (newUser == null) {
      // 这是一个新的会话ID,将创建新的用户
      newUser = new WebUser();
      newUser.setId(sid);
      // 默认为普通用户
      newUser.setNickname("游客" + sid.substring(0, 6));
      newUser.setAccount(sid);
      newUser.setPassword("*");
      newUser.setRule(UserRole.WEB_USER);
      newUser.setRegisterDate(new Date());
      newUser.setScale(1);
      newUser.setReferrer(referrer.getId());
      System.out.println("user:" + JSONObject.toJSONString(newUser));
      this.baseDao.saveOrUpdate(newUser);
    }
    return newUser.getId();
  }

  @Override
  public JSONObject getList(QueryForm form) {
    return null;
  }

  @Override
  public Result delete(DeleteForm form) {
    return null;
  }

  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    return null;
  }

  @Test
  public void test() {
    System.out.println("5F9194BC18ECFDF2A101D3566F5647D9".length());
  }
}
